package com.online.usermanagesystem.dao;

import com.online.usermanagesystem.entity.Config;
import org.apache.ibatis.annotations.*;

import java.util.List;

/**
 * @author jhk
 */

@Mapper
public interface ConfigMapper {

    @Select("select data from ols_config where owner=#{owner} and type=#{type}")
    Config query(@Param("type") String type, @Param("owner") Integer owner);

    @Select("select type,data from ols_config where owner=#{owner}")
    List<Config> list(@Param("owner") Integer owner);

    @Select("select count(*) from ols_config where owner=#{owner} and type=#{type}")
    Integer count(@Param("type") String type, @Param("owner") Integer owner);

    @Insert("insert into ols_config(type,owner,data) values(#{type},#{owner},#{data})")
    @Options(useGeneratedKeys = true)
    Integer insert(Config config);

    @Update("update ols_config set data=#{data} where id=#{id}")
    Integer update(@Param("id") Integer id, @Param("data") String data);

    @Delete("delete from ols_config where id=#{id}")
    Integer delete(Integer id);

    @Select("select id, JSON_EXTRACT(data, #{path}) as data from ols_config where owner=#{owner} and type=#{type}")
    Config jsonQuery(@Param("type") String type, @Param("owner") Integer owner, @Param("path") String path);

    @Update("update ols_config set data=JSON_INSERT(data, #{path}, #{data}) where id=#{id}")
    Integer jsonInsert(@Param("id") Integer id, @Param("path") String path, @Param("data") String data);

    @Update("update ols_config set data=JSON_REPLACE(data, #{path}, #{data}) where id=#{id}")
    Integer jsonUpdate(@Param("id") Integer id, @Param("path") String path, @Param("data") String data);

    @Update("update ols_config set data=JSON_REMOVE(data, #{path}) where id=#{id}")
    Integer jsonDelete(@Param("id") Integer id, @Param("path") String path);
}
